<?php
    header("Content-type:text/html;charset=utf-8");
    $msq = new mysqli("localhost","root","root","zol3c");
    if($msq->connect_errno != 0){
        die();
    }
    $mailbox =  $_POST["mailbox"];
    $password = $_POST["password"];
    $effect = $_POST["effect"];
    if ($effect == "sign"){
        sign($msq,$mailbox,$password);
    }else{
        register($msq,$mailbox,$password);
    }
    function register($msq,$mailbox,$password){
        $quy = $msq->query("select * from user where mailbox='${mailbox}'");
        if(!$quy->num_rows){
            $res = $msq->query("insert into user(mailbox,password) values('${mailbox}','${password}')");
            if($res){
                print_r(json_encode(["success"=>1,"data"=>"注册成功"]));
            }else{
                print_r(json_encode(["success"=>0,"data"=>"服务器未响应"]));
            }
        }else{
            print_r(json_encode(["success"=>0,"data"=>"邮箱已被使用"]));
        }
    }
    function sign($msq,$mailbox,$password){
       $ip = $_POST["ip"] == "yongHu" ? "user" : "shangjia";
       $res = $msq->query("select * from ${ip} where mailbox= '${mailbox}'");
       if($res->num_rows){
            $tis = $res->fetch_assoc();
            if($tis["password"] == $password){
                print_r(json_encode(["success"=>1,"data"=>"登录成功"]));
            }else{
                print_r(json_encode(["success"=>0,"data"=>"登录失败"]));
            }
       }else{
            print_r(json_encode(["success"=>0,"data"=>"未找到该用户"]));
       }
    }
?>